#!/bin/bash
################
### 需要sudo权限
################
set -xeE
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
PS4=$GREEN' [$0:$LINENO] '$NC
trap 'echo -e "$RED ERROR: [$0:$LINENO] $BASH_COMMAND $NC"' ERR
function print_info() {
    echo -e "$BLUE$*$NC"
}

SHELL_PATH=$(dirname "$(readlink -f "$0")")
SRC_PATH=$(dirname "$SHELL_PATH")

MODULE_NAME=${CI_PROJECT_NAME-$(basename $SRC_PATH)}
# ARCHIVE_PATH=$SRC_PATH/archive
# MODULE_PATH=$ARCHIVE_PATH/$MODULE_NAME

PROJECT_VERSION_FILE=$SRC_PATH/version.py
VERSION_GREP_PATTERN='__version__ = "'
VERSION_SED_PATTERN='__version__ = "'
MODULE_VERSION=
ARCHIVE_NAME_PREFIX=
ARCHIVE_NAME=

DEPLOY_TOKEN_MOBILE_USER=ci_deploy_token_mobile
DEPLOY_TOKEN_MOBILE_TOKEN=wfjg-CmFD9PkftXzWX6c
DOCKER_IMAGE_PATH_BRANCH="$CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG-${PRODUCT_ARCH:?}"
DOCKER_IMAGE_PATH_COMMITS="$CI_REGISTRY_IMAGE/commits:$CI_COMMIT_SHORT_SHA-${PRODUCT_ARCH}"
DOCKER_IMAGE_TAGS=(
    $DOCKER_IMAGE_PATH_COMMITS
    $DOCKER_IMAGE_PATH_BRANCH
    ${DOCKER_IMAGE_PATH_BRANCH/repo1.antiy.cn:5443/harbor.antiy.cn}
    ${DOCKER_IMAGE_PATH_COMMITS/repo1.antiy.cn:5443/harbor.antiy.cn}
)

function change_version() {
    # set global var : MODULE_VERSION
    # used by pre_build
    if [ "$CI_JOB_TOKEN" != '' ]; then
        grep -m 1 -E "$VERSION_GREP_PATTERN" $PROJECT_VERSION_FILE
        version_line_no=$(grep "$VERSION_GREP_PATTERN" -m 1 -n $PROJECT_VERSION_FILE | cut -d":" -f1)
        old_product_vesrion=$(grep "$VERSION_GREP_PATTERN" -m 1 $PROJECT_VERSION_FILE | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+')
        product_verison=${old_product_vesrion}-${CI_JOB_ID}.${CI_COMMIT_SHORT_SHA}
        sed -i "$version_line_no"'s/'"${VERSION_SED_PATTERN}${old_product_vesrion}"'/'"${VERSION_SED_PATTERN}${product_verison}"'/g' $PROJECT_VERSION_FILE
        # set global var
        MODULE_VERSION=$(grep "$VERSION_GREP_PATTERN" -m 1 $PROJECT_VERSION_FILE | cut -d'"' -f2 | cut -d'"' -f1)
        test -n $MODULE_VERSION
        test "$MODULE_VERSION" == "$product_verison"
        ARCHIVE_NAME_PREFIX=$MODULE_NAME-$MODULE_VERSION
        ARCHIVE_NAME="$ARCHIVE_NAME_PREFIX.tar.gz"
    fi
}

function build_docker() {
    echo "$CI_REGISTRY"
    docker_image=${DOCKER_IMAGE_TAGS[0]}
    echo $docker_image
    sed -i 's/build_base_arch/build_base_'${PRODUCT_ARCH:?}'/' $SRC_PATH/ops/Dockerfile
    sed -i 's/_CI_COMMIT_SHORT_SHA="xxxxxxxx"/_CI_COMMIT_SHORT_SHA="'$CI_COMMIT_SHORT_SHA'"/g' $SRC_PATH/ops/Dockerfile
    sed -i 's/PRODUCT_ARCH="xxxxxxxx"/PRODUCT_ARCH="'${PRODUCT_ARCH:?product_aarch need}'"/g' $SRC_PATH/ops/Dockerfile
    sed -i 's/MODULE_VERSION="xxxxxxxx"/MODULE_VERSION="'$MODULE_VERSION'"/g' $SRC_PATH/ops/Dockerfile
    sed -i 's/MODULE_INFO="xxxxxxxx"/MODULE_INFO="'${MODULE_NAME:?module_name required}':'${MODULE_VERSION:?MODULE_VERSION required}'"/g' $SRC_PATH/ops/Dockerfile
    docker login -u $DEPLOY_TOKEN_MOBILE_USER -p "$DEPLOY_TOKEN_MOBILE_TOKEN" $CI_REGISTRY
    docker build --network=host --force-rm --pull -t "$docker_image" -f $SRC_PATH/ops/Dockerfile .
    docker push "$docker_image"
    for i in "${DOCKER_IMAGE_TAGS[@]:1}"; do
        echo $i
        docker rmi $i || true
        docker tag $docker_image $i
        docker push $i
    done
}

function build() {
    time build_docker
}

function cache_for_wuhan() {
    test "$CI_PROJECT_NAME" == '' || {
        ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ci@10.251.18.30 mkdir -p /data/ssd01/ci/pipeline/$CI_PROJECT_NAME
        # scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no *.tar.gz ci@10.251.18.30:/data/ssd01/ci/pipeline/$CI_PROJECT_NAME/
        scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no *.jar ci@10.251.18.30:/data/ssd01/ci/pipeline/$CI_PROJECT_NAME/
    }
}

function main() {
    change_version
    time build
    # time cache_for_wuhan
}
main "$@"
